<?php

namespace app\modules\controllers;

use crazyfd\qiniu\Qiniu;
use Yii;
use yii\helpers\FileHelper;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\UploadedFile;


class UploadController extends Controller
{

    public $enableCsrfValidation = false;//禁用Csrf验证

//    private $AK = 'yv-w-hLjxzwjAOOQmw7OlFAqaAnlhIbxAHgaRveK';
//    private $SK = '7UC2KOhmWG_gTwb3B4f13r5ytV-kcbpHq9joCg6r';
//    private $DOMAIN = 'qiniu.harbourhome.com.cn';
//    private $BUCKET = 'harbour';

    private $AK = 'tfmqlymL0sk_6I64Yr1bPAQsjxY9TDp1izav8hOr';
    private  $SK = 'jI3u8GmFp7WYpXRC5-P1xmgmJlH6g7gQPIHUuboU';
    private $DOMAIN = 'pb85r98i8.bkt.clouddn.com';
    private $BUCKET = 'weixiu';



    public function actionQiniu(){
        $this->layout=false;
        if (Yii::$app->request->isPost) {

            $images = UploadedFile::getInstanceByName('cover');
            if (count($images) > 0) {
                if ($images->size > 2048 * 1024) {
                    $res['errCode'] = 1002;
                    $res['errDesc'] = '图片最大不可超过2M.';
                    return json_encode($res);
                }
                if (!in_array(strtolower($images->extension), array('gif', 'jpg', 'jpeg', 'png'))) {
                    $res['errCode'] = 1001;
                    $res['errDesc'] = '请上传标准图片文件, 支持gif,jpg,png和jpeg.';
                    return json_encode($res);
                }
            }
            $pics=$this->upload();
            $res['imgUrl'] = $pics['cover'];
            $imgInfo = getimagesize($_FILES['cover']['tmp_name']);
            $res['imgWidth'] = $imgInfo[0];
            $res['imgHeight'] = $imgInfo[1];
            return json_encode($res);
        }
        return $this->render('uploadqiniu');
    }

    //上传视频用的
    public function actionQiniu_mov(){
        $this->layout=false;
        if (Yii::$app->request->isPost) {
            $pics=$this->upload('movie');
            $res['url']=$pics['movie'];
            $res['size'] = sprintf("%d",$_FILES["movie"]["size"] / 1024);
            return json_encode($res);
        }
        return $this->render('uploadqiniuMov');
    }

    //七牛上传图片
    private function upload($name='cover')
    {
        //考虑加一层条件过滤
        if ($_FILES[$name]['error'] > 0) {
            return false;
        }
        $qiniu = new Qiniu($this->AK, $this->SK, $this->DOMAIN, $this->BUCKET);
        $key = uniqid();

        $qiniu->uploadFile($_FILES[$name]['tmp_name'], $key);//上传
        $cover = $qiniu->getLink($key);//获取图片链接

        return [$name => $cover];
    }


    //单文件上传
    public function actionImage() {
        $this->layout=false;
        if (Yii::$app->request->isPost) {
            $res = [];
            $images = UploadedFile::getInstanceByName('cover');
            if (count($images) > 0) {
                if ($images->size > 2048 * 1024) {
                    $res = ['error' => '图片最大不可超过2M'];
                    return json_encode($res);
                }
                if (!in_array(strtolower($images->extension), array('gif', 'jpg', 'jpeg', 'png'))) {
                    $res = ['error' => '请上传标准图片文件, 支持gif,jpg,png和jpeg.'];
                    return json_encode($res);
                }
                $dir = '/uploads/temp/';
                $pickey=uniqid();
                $filename = $pickey . '.' . $images->getExtension();
                if (!file_exists(Yii::$app->basePath.$dir)) {
                    FileHelper::createDirectory(Yii::$app->basePath.$dir,777);
                }
                $filepath = realpath(Yii::$app->basePath.$dir) . '/';
                $file = $filepath . $filename;
                if($images->saveAs($file)) {
                    $imgpath = $dir . $filename;
                    $res['imgUrl']=$imgpath;
                    return json_encode($res);
                }
            }
        }
        return $this->render('upload');
    }




}